/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;

import MODEL.POJO.DanhMucPOJO;
import MODEL.POJO.SanPhamPOJO;
import MODEL.UTIL.MySqlDataAccessHelper;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author kijoo
 */
public class SanPhamDAO {

    public static ArrayList<SanPhamPOJO> LayDanhSachSanPham(String iddanhmuc) {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            String sql = String.format("SELECT * FROM SANPHAM WHERE IDDANHMUC='%s'", iddanhmuc);
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setIdsanpham(rs.getString("idsanpham"));
                sp.setIddanhmuc(rs.getString("iddanhmuc"));
                sp.setDongsanpham(rs.getString("dongsanpham"));
                ds.add(sp);
            }
            helper.Close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    public static SanPhamPOJO LaySP(String idsanpham) {
        SanPhamPOJO sp = new SanPhamPOJO();
        String sql = String.format("SELECT IDSANPHAM,DONGSANPHAM FROM SANPHAM WHERE IDSANPHAM='%s' ", idsanpham);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                sp.setDongsanpham(rs.getString("DONGSANPHAM"));
                sp.setIdsanpham(rs.getString("IDSANPHAM"));
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return sp;
    }
    
    public static ArrayList<SanPhamPOJO> LayDanhSachDongSanPham(String iddanhmuc) {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            String sql = String.format("SELECT * FROM SANPHAM WHERE IDDANHMUC='%s'", iddanhmuc);
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setIdsanpham(rs.getString("idsanpham"));
                sp.setIddanhmuc(rs.getString("iddanhmuc"));
                sp.setDongsanpham(rs.getString("dongsanpham"));
                ds.add(sp);
            }
            helper.Close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    
    public static boolean ThemSanPham(String idSP, String idDM, String dongSP) {
        boolean kq = false;
        try {
            String sql = String.format("INSERT INTO SANPHAM(IDSANPHAM, IDDANHMUC, DONGSANPHAM) VALUES('%s', '%s', '%s')", idSP, idDM, dongSP);
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return kq;

    }

}
